<?php

namespace Project\ScrumBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Project\ScrumBundle\Entity\Userstories;
use Project\ScrumBundle\Form\UserstoriesType;
use Project\ScrumBundle\Classe\StatsEnum;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BacklogController
 *
 * @author jvida_000
 */
class ClientController extends Controller {
    
    
    /**
     * Fonction servant a afficher au client sa page d'ajout d'userstories
     * @return type
     */
    public function userStoriesAction() {

        $session = $this->get('session');
        //Si l'utilisateur est un client on affiche la page
        if ($session->get('role') == "ROLE_CLIENT") {
            $userstories = new Userstories;

            $UserstoriesType = new UserstoriesType();
            $form = $this->createForm($UserstoriesType, $userstories, array('attr' => (array($session->get('id_user')))));

            // On récupère la requête
            $request = $this->get('request');
            $em = $this->getDoctrine()->getManager();
            // On vérifie qu'elle est de type POST
            if ($request->getMethod() == 'POST') {
                // On fait le lien Requête <-> Formulaire
                $form->bind($request);

                $client = $em->getRepository('ProjectScrumBundle:Utilisateur')->find($session->get('id_user'));
                $userstories->setRefClient($client);
                $userstories->setPtsDifficulte(0);
                $userstories->setAffectation(false);
                $userstories->setStatut(StatsEnum::NON_COMMENCE);

                if ($userstories->getRefProjet() != null) {
                    // On l'enregistre notre objet dans la base de données
                    $em->persist($userstories);
                    $em->flush();
                }
                //ici on recupère toutes les user stories du client 
                $userstoriesdb = $em->getRepository('ProjectScrumBundle:Userstories')->findBy(array('refClient' => $session->get('id_user')));


                // On redirige vers la page d'accueil

                return $this->render('ProjectScrumBundle:Client:userstories.html.twig', array('form' => $form->createView(), 'userstories' => $userstoriesdb));
            }
            //ici on recupère toutes les user stories du client 
            $userstoriesdb = $em->getRepository('ProjectScrumBundle:Userstories')->findBy(array('refClient' => $session->get('id_user')));

            return $this->render('ProjectScrumBundle:Client:userstories.html.twig', array('form' => $form->createView(), 'userstories' => $userstoriesdb));
        } else {
            return $this->redirect($this->generateUrl('project_authentification_login'));
        }
    }

}

?>
